#include "StdAfx.h"
#include "DbHelper2.h"

CDbHelper2::CDbHelper2(void)
	: m_bConnect(FALSE)
{
}

CDbHelper2::~CDbHelper2(void)
{
}

void CDbHelper2::TraceError(LPCTSTR caller, _com_error & e)
{
// 	CString strError;
// 	strError.AppendFormat(">JTJ %s ---- DBHelperBase caller %s fail, error detail:\n", GetServerName(), caller);
// 	strError.AppendFormat(">JTJ %s ---- \a\t Code = %08x\n", GetServerName(), e.Error());
// 	strError.AppendFormat(">JTJ %s ---- \a\t Code meaning = %s\n", GetServerName(), e.ErrorMessage());
// 	_bstr_t bstrSource(e.Source());
// 	_bstr_t bstrDescription(e.Description());
// 	strError.AppendFormat(">JTJ %s ---- \a\t Source = %s\n", GetServerName(), (LPCTSTR)bstrSource);
// 	strError.AppendFormat(">JTJ %s ---- \a\t Description = %s\n", GetServerName(), (LPCTSTR)bstrDescription);
// 
// 	DbLog.WriteLevelLog(emLOG_ERROR, strError);
//	OutputDebugString(strError);
}


BOOL CDbHelper2::InitConnection(CString strConnect)
{
	try
	{
		HRESULT hr = m_pConnection.CreateInstance(__uuidof(Connection));
		if (SUCCEEDED(hr))
		{
			m_pConnection->Open((_bstr_t)strConnect, "", "", adModeUnknown);
			m_bConnect = TRUE;
			return TRUE;
		}
	}
	catch (_com_error & e)
	{
		TraceError("GetConnectionPtr: connect database fail", e);
	}

	return FALSE;
}

BOOL CDbHelper2::GetStreamInfo(int nCameraId, Struct_CamreaInfo &streamInfo)
{
	if (!m_bConnect)
	{
		return FALSE;
	}
	try
	{
		_RecordsetPtr pRecordSet;		
		HRESULT hr = pRecordSet.CreateInstance(__uuidof(Recordset));

		if (SUCCEEDED(hr))
		{
			CString strSQL;			
			// get the stream server
			strSQL.Format(_T("select r.ID,r.strName,r.nRegID, r.nChanNum,r.nStreamType,d.nDevType,r.nCamType,d.strIP,d.strUser,d.strPassword,d.nPort,isnull(s.strIP,'') as PrxoyIP,isnull(s.nCtrlPort,'0') as nCtrlPort,isnull(table1.strIP,'') as StreamIP,isnull(table1.nDataPort,'0') as StreamPort ,ISNULL(s.nDataPort,'0') as nDataPort,d.strDeviceID from RegionRes r \
							 inner join Device d on d.ID = r.nDevID left join Server s on s.ID = d.nProxyID left join (select s.strUser,s.strIP,s.nDataPort,r.nRegID from RegionServer r right join Server s on s.ID = r.nSerID where s.nType = 0) table1 \
							 on table1.nRegID = r.nRegID where r.nType = 0 and r.ID = %d"), nCameraId);
			pRecordSet->Open((_bstr_t)strSQL, _variant_t((IDispatch *)m_pConnection, true),
				adOpenForwardOnly, adLockReadOnly, adCmdText);

			if(!pRecordSet->adoEOF)
			{
				streamInfo.ID = pRecordSet->GetCollect("ID").intVal;
				streamInfo.strIP = pRecordSet->GetCollect("strIP").bstrVal;
				streamInfo.strName = pRecordSet->GetCollect("strName").bstrVal;
				streamInfo.strUser = pRecordSet->GetCollect("strUser").bstrVal;
				streamInfo.strPassword = pRecordSet->GetCollect("strPassword").bstrVal;
				streamInfo.nChannel = pRecordSet->GetCollect("nChanNum").intVal;
				streamInfo.nRealType = pRecordSet->GetCollect("nStreamType").intVal;
				streamInfo.nPort = pRecordSet->GetCollect("nPort").intVal;
				streamInfo.strProxyIP = pRecordSet->GetCollect("PrxoyIP").bstrVal;
				streamInfo.nProxyPort = pRecordSet->GetCollect("nCtrlPort").intVal;
				streamInfo.strStreamIP = pRecordSet->GetCollect("StreamIP").bstrVal;
				streamInfo.nStramPort = pRecordSet->GetCollect("StreamPort").intVal;
				streamInfo.nDevType = pRecordSet->GetCollect("nDevType").intVal;
				streamInfo.nCamType = pRecordSet->GetCollect("nCamType").intVal;

				//add by llh begin 2011-04-19
				streamInfo.strDeviceID = pRecordSet->GetCollect("strDeviceID").bstrVal;
				streamInfo.nAccessPort = pRecordSet->GetCollect("nDataPort").intVal;
				//add by llh end
				pRecordSet->MoveNext();
				pRecordSet->Close();
			}
			else
			{
				pRecordSet->Close();
				return FALSE;
			}

		}
	}
	catch (_com_error & e)
	{
		TraceError("GetStreamInfo", e);
		return FALSE;
	}
	return TRUE;
}